<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Attribute;
use App\Models\Good;
use App\Models\GoodAttribute;
use App\Models\GoodInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class GoodAttributeController extends Controller
{
    public function all($goodId)
    {
        if (!$this->checkGood($goodId)) {
            return jsonResponse(40000);
        }
        $attributes = GoodAttribute::where('good_id', $goodId)->get();
        return jsonResponse(config('common.successCode', $attributes));
    }

    public function create(Request $request, $goodId)
    {
        if (!$this->checkGood($goodId)) {
            return jsonResponse(40000);
        }

        $validator = $this->validate($request);
        if ($validator->fails()) {
            return jsonResponse(config('common.validatorError'), $validator);
        }

        $attrId = $request->get('attr');
        $attribute = Attribute::find($attrId);
        if (empty($attribute)) {
            return jsonResponse(40000);
        }
        $value = $request->get('value');

        $attr = new GoodAttribute();
        $attr['good_id'] = $goodId;
        $attr['attr_id'] = $attrId;
        $attr['value'] = $value;
        $attr->save();
        return jsonResponse();
    }

    public function update(Request $request, $goodId, $id)
    {
        if (!$this->checkGood($goodId)) {
            return jsonResponse(40000);
        }
        $validator = $this->validate($request);
        if ($validator->fails()) {
            return jsonResponse(config('common.validatorError'), $validator);
        }

        $attrId = $request->get('attr');
        $attribute = Attribute::find($attrId);
        if (empty($attribute)) {
            return jsonResponse(40000);
        }

        $value = $request->get('value');

        $attr = GoodAttribute::find($id);
        if (empty($attr)) {
            return jsonResponse(40000);
        }
        $attr['good_id'] = $goodId;
        $attr['attr_id'] = $attrId;
        $attr['value'] = $value;
        $attr->save();
        return jsonResponse();
    }

    public function destroy($goodId, $id)
    {
        if (!$this->checkGood($goodId)) {
            return jsonResponse(40000);
        }
        // 删除商品属性，需要删除该属性对应的商品信息
        $items = GoodInfo::where('good_id', $goodId)->get();
        foreach ($items as $item) {
            $ids = explode(config('common.delimiter'), $item['uid']);
            foreach ($ids as $attrId) {
                if ($attrId == $id) {
                    $item->delete();
                    break;
                }
            }
        }
        GoodAttribute::destroy($id);
        return jsonResponse();
    }

    private function validate($request)
    {
        return Validator::make($request->all(), [
            'value' => 'required',
        ], [
            'value.required' => '属性值不能为空',
        ]);
    }

    private function checkGood($id)
    {
        $good = Good::find($id);
        if (empty($good)) {
            return false;
        }
        return true;
    }
}
